package com.szrt.javaweb;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.szrt.javaweb.entity.UserEntity;




public class LoginController extends HttpServlet {

	private static final long serialVersionUID = -8485448625646581739L;
	
	
	private ServletConfig config;
	
	@Override
	public void init(ServletConfig config) throws ServletException {
		this.config=config;
		
	}	
	
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		
		
		
		String method =request.getParameter("method");
		
		if("go".equals(method)){ //1 为了避免空指针异常
			
			//http://localhost:8080/mvn-JavaWeb-Cookie/loginController.do?method=go 前缀条件
			go(request,response);
			
		}else if("loginIn".equals(method)){
			//说明要处理真正的登录的逻辑
			loginIn(request,response);
			
		}else if("logout".equals(method)){
		//说明要处理真正的登录的逻辑
			logout(request,response);
		
	}
		
		
	}

	private void logout(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException{
		request.getSession().invalidate();
		System.out.println(request.getSession(false));
		
		sendRedirect(response,"/loginController.do?method=go");
		
		
		
	}
	private void loginIn(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException{
		
		//1先得到用户名和密码
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		
		
		if(null !=username  && null !=password  && !"".equals(username)  && !"".equals(password)){
			if("szrt".equals(username) && "123456".equals(password)){
				//2 如果用户名和密码正确   并且 把用户信息保存在session中。跳到success.jsp
				
				//2.1 就强制生成一个session
				HttpSession session = request.getSession(true);
				
				//2.2 把用户信息保存在session中  key  value  在开发中要key要写一个固定的值
 				session.setAttribute("zxx", new UserEntity(username,password));
 				session.setAttribute("JSESSIONID", session.getId());
				
				//2.3跳到loginSuccess.jsp
				sendRedirect(response,"/login/loginSuccess.jsp");
				
			}else{
				
				
				//3 如果用户名和密码正确  	跳到login.jsp 
				//因为登录的时候已经强制生成了session并且把user放入到HttpSession作用域中 
				//只要用户不关闭浏览器  在任何地方都可以访问到
				sendRedirect(response,"/login/loginPage.jsp");
			}
		}
	}

	private void go(HttpServletRequest request, HttpServletResponse response) 
			
			throws ServletException, IOException{
		
		//1 从request拿到session 此处不应该生成会话session  如果有就拿  如果没有返回null
		HttpSession session = request.getSession(false);
		
		
		if(null == session ){
			
			//说明没有登录  loginPage.jsp
			
			//怎么拿到ServeltContext
			
			//重定向
			sendRedirect(response,"/login/loginPage.jsp");
		}else{
			
			//说明有登录过  loginSuccess.jsp
			sendRedirect(response,"/login/loginSuccess.jsp");
			
			
		}
	}

	private void sendRedirect(HttpServletResponse response,String path) throws IOException {
		ServletContext context=config.getServletContext();
		String root = context.getContextPath();
		
		//http://localhost:8080/mvn-JavaWeb-Cookie/login/loginPage.jsp
		String location = root +path;
		System.out.println(location);
		
		response.sendRedirect(location);
	}

}
